<template>
  <div class="login">

    <div class="kj">
        <div><img src="../assets/images/login-cha.png"/></div>
      <div>
        <div class="ipt">
         <div class="ipt_logo">
              <img src="../assets/images/login-logo.png"/>
         </div>
          <div class="ipt_one">
            <img src="../assets/images/tel.png" class="ico"/>
            <input class="innput1" type="tel"  maxlength="11" v-model="tel"  placeholder="请输入手机号"/>
            <img src="../assets/images/xian.png" class="xian"/>
          </div>
          <div class="ipt_two">
            <img src="../assets/images/key.png" class="ico"/>
            <input class="innput2" type="tel" maxlength="6" v-model="verificationCode" placeholder="验证码"/> <span class="getcode" @click="getCode()">{{obtain}}</span>
            <img src="../assets/images/xian.png" class="xian"/>
          </div>
          <div class="ipt_btn">
            <Button class="bbtn" type="primary" shape="circle" :loading="loading" @click="toLoading">
              <span v-if="!loading">登录</span>
              <span v-else>登录中...</span>
            </Button>
          </div>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
  export default {
    data() {
      return {
        tel:"",
        loading: false,
        obtain:'获取验证码',
        totalTime:60,
        verificationCode:'',
      }
    },
    methods: {
      toLoading () {
        this.loading = true;
        this.$axios({
          url: 'web/user/login',
          method: 'POST',
          data:{
            phone:this.tel,
            verifyCode:this.verificationCode
          },
          headers: {
            'Content-Type': 'application/json;charset=UTF-8'
          },
          transformRequest: [function(data) {  //json
            data = JSON.stringify(data);
            return data
          }],
        }).then((response) => {
          console.log(response.data);
          if(response.data.code==500){
            this.$Message.error(response.data.msg);
            this.loading = false;
          }else{

            this.$cookies.set('pcexam_id', response.data.id,60*60*24*30);//用户id
            this.$cookies.set('pcexam_username', response.data.username,60*60*24*30);//用户名
            this.$cookies.set('pcexam_name', response.data.name,60*60*24*30);//用户名
            this.$cookies.set('pcexam_phone', response.data.phone,60*60*24*30);//电话
            this.$cookies.set('pcexam_position', response.data.position,60*60*24*30);//职务
            this.$cookies.set('pcexam_departmentId', response.data.departmentId,60*60*24*30);//部门id
            this.$router.push('/index')
          }


        }).catch((error) => {
          console.log("调取失败500");
          this.$Message.error('登陆失败！')
        });
      },
      getCode(){
     console.log(this.tel);
        if(!(/^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(this.tel))){
          this.$Message.warning('手机号有误无法获取');
        }else {
          if(this.obtain=='获取验证码'||this.obtain=='重新获取验证码'){
            this.ajjax();
          }
        }
      },
      ajjax(){
        this.$axios({
          url: 'web/user/login/getVerifyCode',
          method: 'POST',
          data:{
            phone:this.tel,
          },
          headers: {
            'Content-Type': 'application/json;charset=UTF-8'
          },
          transformRequest: [function(data) {  //json
            data = JSON.stringify(data);
            return data
          }],
        }).then((response) => {
          console.log(response.data);
          if(response.data.msg=='success'){
            this.$Message.success('获取验证码成功！')
            let clock = window.setInterval(() => {
              this.totalTime--;
              this.obtain = this.totalTime + 's后重新发送';
              if (this.totalTime < 1) {
                window.clearInterval(clock);
                this.obtain = '重新获取验证码';
                this.totalTime=60;
              }
            },1000)
          } else {
            this.$Message.error('获取验证码失败：'+response.data.msg)
          }
        }).catch((error) => {
          console.log("调取失败500");
          console.log('erro',error);
        });
      },
    }


}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  input::-webkit-outer-spin-button,
  input::-webkit-inner-spin-button {
    -webkit-appearance: none;
  }
  input[type="number"] {
    -moz-appearance: textfield;
  }
  .login{
    padding: 0;
    margin: 0;
    width: 100vw;
    height: 100vh;
    background-image: url('../assets/images/bj.jpg');
    background-size: cover;
    position: relative;
  }
  .kj{
    position: absolute;
    top:50%;
    left: 50%;
    margin-left: -510px;
    margin-top: -300px;
    width:1020px;
    box-sizing: border-box;
    height: 600px;
    background: white;
    border-radius:10px;
  }
  .kj>div{
    width: 510px;

    box-sizing: border-box;
    float: left;
  }
  .kj>div:nth-of-type(1) img{
    width: 438px;
    height: 379px;
    margin-top: 115px;
    margin-left: 91px;
  }
  .kj>div:nth-of-type(2){
    padding-left: 100px;
  }
  .ipt_logo{
    margin-bottom: 45px;
    margin-top: 143px;
    margin-left:-35px;
  }
  .bbtn{
    width:259px;
    height:60px;
    font-size: 24px;
    margin-top: 62px;
  }
  .ipt_one{
    height:40px;
    position: relative;
    margin-bottom: 30px;
  }
  .ipt_two{
    height: 40px;
    position: relative;
  }
  .innput1{
    font-size: 17px;
    width:170px ;
    height: 40px;
    border: none;
    outline:none;
  }
  .innput2{
    font-size: 17px;
    width:115px ;
    height: 30px;
    border: none;
    outline:none;
  }
  .xian{position: absolute;
    bottom: 0px;
    left: 0;
    width: 299px;
    height: 1px;
  }
  .ico{
    width: 17px;
    height: 22px;
    margin-right: 11px;
    margin-bottom: -5px;
  }.getcode{
    color: #17A1E8;
       cursor: pointer;
    font-size: 17px;
     }

</style>
